﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Global;
using System.Data.OleDb;
using AccessOperator;
using BussinessLib;
using Entity;
using System.Text;

/// <summary>
/// RestaurantService 的摘要说明
/// </summary>
public class RestaurantService:IRestaurantService
{
	public RestaurantService()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    public string reserve(string username, string password, string restaId, string startTime, int category, int numbers){
        OrderServiceManager OrderService = new OrderServiceManager();
        if (OrderService.testUserInfo(username,password) != 0){
            if (category < 1 | category > 3 | numbers < 1)
                return null;
            else {
         
                DataTable m_DataTable = new DataTable();
                Helper m_DataAccess = new Helper();

                if (numbers <= OrderService.SeatNum(category, restaId, startTime))
                {
                    Order record = new Order();
                    
                    OrderService.createOrder(record, username, restaId, startTime, category, numbers);
                    StringBuilder sb = new StringBuilder();
                    sb.Append("insert into OrderRecord (ord_dtBookTime,ord_dtArrivalTime,ord_nTableNum,ord_nState,ord_nUserID) values(");
                    sb.Append("'" + record.BookTime + "','" + record.ArrivalTime + "'," + record.TableNum + "," + record.TableID + "'," + record.UserID + ");");//tableID为餐馆号

                    m_DataAccess.ExecuteSql(sb.ToString());

                    string str = "select MAX(ord_nID) from OrderRecord;";
                    m_DataTable = m_DataAccess.ExecuteDataTable(str);

                    string orderno = m_DataTable.Rows[0][0].ToString();

                    return orderno;

                }
                else return null;
            }
        }
        else 
            return null;
    
    
    }
    public int cancel(string username, string password, string orderId) {
        OrderServiceManager OrderService = new OrderServiceManager();
        if (OrderService.testUserInfo(username, password) != 0){
            string strSql = "select ord_nID from OrderRecord where ord_nID = ";
            strSql += orderId;
           
            DataTable m_DataTable = new DataTable();
            Helper m_DataAccess = new Helper();
            m_DataTable = m_DataAccess.ExecuteDataTable(strSql);
            if(m_DataTable.Rows[0][0].ToString() != null){
                
                strSql = "delete * from OrderRecord where ord_nID = ";
                strSql += orderId;
                string strSql1 = "delete * from OrderTableSet where OTS_nOderID = ";
                strSql1 += orderId;
                m_DataAccess.ExecuteSql(strSql);
                m_DataAccess.ExecuteSql(strSql1);
                return 1;
            }
            
            else return 0;
        }
        else return -1; 
    
    }
    /*
    public int testUserInfo(string username, string password)
    {
        string strSql = "select * from [User] where [U_sName] like '";
        strSql += username  + "' and [U_sPwd] like '";
        strSql += password + "'";

        try
        {
            string strPath = ConfigurationManager.ConnectionStrings["StrConn"].ToString();
            CAccessOperator dboperator = new CAccessOperator(strPath);

            //Execute the SQL if everything is well return true
            //else return false
            OleDbDataReader dr = dboperator.DbRead(strSql);
            int uId = 0;
            if (dr.Read())
            {
                //Role = int.Parse(dr["U_nRole"].ToString());
                uId = int.Parse(dr["U_nID"].ToString());
            }
            dboperator.CloseDb();
            return uId;

        }
        catch (Exception e)
        {
            return 0;
        }



    }
    public int SeatNum(int iAttriID, string iResId, string startTime)
    {
        string strSql;

       // strSql = "select   from orderRecord  "
        strSql = "select count(*)  from Seat where T_nAttributeID = " + iAttriID
            + "and T_nResID =" + iResId;
        DataTable m_DataTable2 = new DataTable();
        Helper m_DataAccess2 = new Helper();
        m_DataTable2 = m_DataAccess2.ExecuteDataTable(strSql);
        int allnumber = int.Parse(m_DataTable2.Rows[0][0].ToString());

        strSql = "select sum(ord_nTableNum)  from OrderRecord where T_nAttributeID = " + iAttriID
            + "and T_nResID = " + iResId + "and ord_dtArrivalTime = " + startTime;
        m_DataTable2 = m_DataAccess2.ExecuteDataTable(strSql);
        //strSql = "select count(*) from m_DataTable2";
        //m_DataTable2 = m_DataAccess2.ExecuteDataTable(strSql);
        int orderednumber = int.Parse(m_DataTable2.Rows[0][0].ToString());



        int number = allnumber - orderednumber;
        return number;
    }
    public void createOrder(Order record, string username, string restaId, string startTime, int category, int numbers) {
        
 
        
        record.ArrivalTime = DateTime.Parse(startTime);
        record.BookTime = DateTime.Now;
        record.TableNum = numbers;
        //record.TableID = int.Parse(Request.QueryString[0]);

        record.TableType = category;

        //record.Description = this.txt_Memo.Text;
        record.UserID =int.Parse(username);

     
    }

}
/*class test {
    public void Main(String[] args) {
        RestaurantService rs = new RestaurantService();
        string order = rs.reserve("admin", "admin", "1",  "2007-11-11 11:11", 1, 5);
        System.Console.Out.WriteLine(order);
    }*/
}